from matplotlib.pylab import plt
import numpy as np


# 获取数据精度
def forecastPrecise_classify(predict: list[float], real: list[float]) -> float:
    size = min(len(predict), len(real))
    score = size
    for i in range(size):
        if predict[i] != real[i]:
            score -= 1
    return score / size


# 字符转化
def TransformLuoma(char: str | np.float64) -> str | np.float64:
    map = {'Ⅰ': np.float64(1.0),
           'Ⅱ': np.float64(2.0),
           'Ⅲ': np.float64(3.0),
           'Ⅳ': np.float64(4.0),
           'Ⅴ': np.float64(5.0)}
    if type(char) == str and char in map:
        return map.get(char)
    elif type(char) == np.float64:
        for i in map:
            if map.get(i) == char: return i
    return ''


def getPlt_R_C(n: int):
    r, c, minv = 1, n, 1 + n
    for i in range(1, n+1):
        if n%i==0 and i+(n/i)<minv:
            r,c=i,n/i
            minv = r+c
    return int(r),int(c)


# 绘制图像
def drawGraph(data: list[dict], Title: str):
    row, column = getPlt_R_C(len(data))
    plt.rcParams['font.family'] = 'SimHei'
    for index, item in enumerate(data):
        x, r_y, p_y, title = item.get("x"), item.get('r_y'), item.get("p_y"), item.get("title")
        plt.subplot(row, column, index + 1)
        plt.plot(x, r_y, label="真实的数据")
        plt.scatter(x, r_y)
        plt.plot(x, p_y, label="预测的数据")
        plt.scatter(x, p_y)
        plt.legend()
        plt.title(title)
    plt.suptitle(Title)
    plt.show()


if __name__ == "__main__":
    data = [
        {"title": "水温预测对照图", "x": [1, 2, 3, 4, 5, 6, 7], "r_y": [7.8, 8.2, 11.0, 10.5, 8.7, 9.2, 10.1],
         "p_y": [8.1, 8.5, 9.8, 10.2, 9.0, 9.2, 10.5]},
        {"title": "ph值预测对照图", "x": [1, 2, 3, 4, 5, 6, 7], "r_y": [7, 8, 8, 8, 7, 7, 8],
         "p_y": [7, 7.6, 7.5, 8, 7.5, 8, 8]},
        {"title": "电导率预测对照图", "x": [1, 2, 3, 4, 5, 6, 7],
         "r_y": [822.8, 875.6, 862.4, 890.4, 887.6, 895.5, 880.4],
         "p_y": [832.8, 865.6, 875.4, 887.2, 894.3, 890.5, 882.7]},
        {"title": "溶解氧预测对照图", "x": [1, 2, 3, 4, 5, 6, 7], "r_y": [11.8, 14.6, 12.3, 18.2, 17.5, 16.4, 17.9],
         "p_y": [12.1, 15.1, 13.0, 19.8, 18.2, 15.8, 18.1]}
    ]
    drawGraph(data, Title="水质指标预测分析图")
